import http from '@/support/http'
import { Code } from '@/enum/app'
import Message from '@/support/message'
import { ref, watch } from 'vue'
import { isFunction } from '@/support/helper'
import { ElMessage, ElMessageBox } from 'element-plus'

export function useCloses(confirm: string = '确认关闭吗') {
  const isDeleted = ref(false)

  const beforeDestroy = ref()

//   fetch list
  function closes(path: string, id: string | number) {


    ElMessageBox.prompt('请输入关闭原因', '关闭订单', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern:/\S+/,
        inputErrorMessage: '原因不能为空',
    }).then(({ value }) => {
          http
            .post(path + '/close',{open_message:value,id:id})
            .then(r => {
                if (r.data.code === Code.SUCCESS) {
                    Message.success(r.data.message)
                    isDeleted.value = true
                } else {
                    Message.error(r.data.message)
                }
            })
            // ElMessage({
            //     type: 'success',
            //     message: `关闭订单成功`,
            // })
        })
        .catch(() => {
            // ElMessage({
            //     type: 'info',
            //     message: 'Input canceled',
            // })
        })
    // Message.confirm(confirm + '?', function () {
    //   // before destroy
    //   if (isFunction(beforeDestroy.value)) {
    //     beforeDestroy.value()
    //   }

    //   http
    //     .put(path + '/' + id)
    //     .then(r => {
    //       if (r.data.code === Code.SUCCESS) {
    //         Message.success(r.data.message)
    //         isDeleted.value = true
    //       } else {
    //         Message.error(r.data.message)
    //       }
    //     })
    //     .finally(() => {})
    // })
  }

  const deleted = (reset: Function) => {
    watch(isDeleted, function (value) {                                             
      if (value) {
        isDeleted.value = false
        reset()
      }
    })
  }

  return { closes, deleted }
}
